home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / pd mix ii / access / thai / tsentence.c < prev    next >
C/C++ Source or Header  |  1994-05-20  |  3KB  |  133 lines

  1.  
  2. #include "quiz.h"
  3.  
  4.  
  5. #define MAX_SPLIT_WORDS        30
  6. #define DEBUG(parms)
  7.  
  8.  
  9. int num_split_words = 0;
  10. int cur_split_word = 0;
  11. struct thai_phrase *split_words[ MAX_SPLIT_WORDS ];
  12.  
  13.  
  14. struct thai_phrase *
  15. dict_search ( sentence , cur_try )
  16. char *sentence;            /* sentence of thai characters */
  17. struct thai_phrase *cur_try;    /* current try, NULL means first try */
  18. {
  19.     struct thai_phrase *best , *p;
  20.     int best_len , past_cur_try , max_len;
  21.     int i;
  22.  
  23.  
  24.     if ( cur_try != NULL )
  25.         max_len = strlen ( cur_try->thai );
  26.     else
  27.         max_len = 1000;
  28.     past_cur_try = FALSE;
  29.     best = NULL;
  30.     best_len = 0;
  31.  
  32.     /* ok, now we scan through the dictionary looking for the longest */
  33.     /* entry which is either before and shorter than the current try */
  34.     /* or else after and shorter than or equal to the current try. */
  35.     /* Looking for the longest entry first means that longer phrases */
  36.     /* in the dictionary will be used first (they are more likely to */
  37.     /* be accurate) */
  38.  
  39.     for ( p = word_head.next; p != NULL; p = p->next ) {
  40.  
  41.         /* compare sentence to thai phrase */
  42.  
  43.         for ( i = 0; p->thai[i] != '\0'; i++ ) {
  44.             if ( sentence[i] != p->thai[i] )
  45.                 break;
  46.         }
  47.  
  48.         if ( p->thai[i] == '\0' ) {
  49.  
  50.             /* all of phrase matched start of sentence */
  51.  
  52.             if ( i > best_len ) {    /* otherwise dont bother */
  53.                 if ( past_cur_try ) {
  54.                     if ( i <= max_len ) {
  55.                         best = p;
  56.                         best_len = i;
  57.                     }
  58.                 }
  59.                 else {
  60.                     if ( i < max_len ) {
  61.                         best = p;
  62.                         best_len = i;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.         if ( p == cur_try )
  68.             past_cur_try = TRUE;
  69.     }
  70.     if ( best == NULL ) {
  71.         DEBUG ( ( "best is null! scan for %s\n" , sentence ) );
  72.     }
  73.     else {
  74.         DEBUG ( ( "scan for %s -- best is %s (%s)\n" , sentence , best->english , best->thai ) );
  75.     }
  76.     return ( best );
  77. }
  78.  
  79.  
  80.  
  81. /* returns NULL if it works, or else a pointer to the bit it does not know */
  82.  
  83. char *
  84. split_sentence ( sentence )
  85. char *sentence;
  86. {
  87.     char *parse;
  88.     char *best_parse;    /* keep track of longest parse made */
  89.     struct thai_phrase *p;
  90.     int cur_word;
  91.  
  92.  
  93.     best_parse = sentence;
  94.     parse = sentence;
  95.     split_words[0] = NULL;    /* mark as look for first entry */
  96.     cur_word = 0;
  97.  
  98.     while ( cur_word >= 0 ) {
  99.         DEBUG ( ( "Scanned up to char %d\n" , parse - sentence ) );
  100.         if ( split_words[ cur_word ] != NULL )
  101.             parse -= strlen ( split_words[ cur_word ]->thai );
  102.         DEBUG ( ( "backed up to char %d\n" , parse - sentence ) );
  103.         p = dict_search ( parse , split_words[ cur_word ] );
  104.         if ( p != NULL ) {
  105.             parse += strlen ( p->thai );
  106.             DEBUG ( ( "skipped forward up to char %d\n" , parse - sentence ) );
  107.             if ( parse > best_parse )
  108.                 best_parse = parse;
  109.             split_words[ cur_word ] = p;
  110.             cur_word++;
  111.             DEBUG ( ( "cur word ++ now %d\n" , cur_word ) );
  112.             split_words[ cur_word ] = NULL;
  113.             if ( *parse == '\0' ) {
  114.                 num_split_words = cur_word;
  115.                 cur_split_word = 0;
  116.                 DEBUG ( ( "GOT IT! %d words\n" , num_split_words ) );
  117.                 return ( NULL );    /* DONE! */
  118.             }
  119.         }
  120.         else {
  121.             /* search failed, back up a word and try something else */
  122.             cur_word--;
  123.             DEBUG ( ( "cur word -- now %d\n" , cur_word ) );
  124.         }
  125.     }
  126.  
  127.     /* failed, return pointer to likey word that was not in dictionary */
  128.     /* and let user try to work out what word was not known */
  129.  
  130.     DEBUG ( ( "Failed: closest was %d chars\n" , best_parse - sentence ) );
  131.     return ( best_parse );
  132. }
  133.